package com.changgou.user.controller;

import com.changgou.entity.Result;
import com.changgou.entity.StatusCode;
import com.changgou.goods.pojo.Spu;
import com.changgou.user.pojo.Collect;
import com.changgou.user.service.CollectService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author CJ
 * @date 2020/5/29
 */
@RestController
@RequestMapping("/collect")
@CrossOrigin
public class CollectController {
    @Autowired
    private CollectService collectService;

    /***
     * Collect分页条件搜索实现
     * @param collect
     * @param page
     * @param size
     * @return
     */
    @PostMapping(value = "/search/{page}/{size}" )
    public Result<PageInfo> findPage(@RequestBody(required = false) Collect collect, @PathVariable  int page, @PathVariable  int size){
        //调用CollectService实现分页条件查询Collect
        PageInfo<Collect> pageInfo = collectService.findPage(collect, page, size);
        return new Result(true, StatusCode.OK,"查询成功",pageInfo);
    }

    /***
     * Collect分页搜索实现
     * @param page:当前页
     * @param size:每页显示多少条
     * @return
     */
    @GetMapping(value = "/search/{page}/{size}" )
    public Result<PageInfo> findPage(@PathVariable  int page, @PathVariable  int size){
        //调用CollectService实现分页查询Collect
        PageInfo<Collect> pageInfo = collectService.findPage(page, size);
        return new Result<PageInfo>(true,StatusCode.OK,"查询成功",pageInfo);
    }

    /***
     * 多条件搜索品牌数据
     * @param collect
     * @return
     */
    @PostMapping(value = "/search" )
    public Result<List<Collect>> findList(@RequestBody(required = false)  Collect collect){
        //调用CollectService实现条件查询Collect
        List<Collect> list = collectService.findList(collect);
        return new Result<List<Collect>>(true,StatusCode.OK,"查询成功",list);
    }

    /***
     * 根据ID删除品牌数据
     * @param id
     * @return
     */
    @DeleteMapping(value = "/{id}" )
    public Result delete(@PathVariable String id){
        //调用CollectService实现根据主键删除
        collectService.delete(id);
        return new Result(true,StatusCode.OK,"删除成功");
    }

    /***
     * 修改Collect数据
     * @param collect
     * @param id
     * @return
     */
    @PutMapping(value="/{id}")
    public Result update(@RequestBody  Collect collect,@PathVariable String id){
        //设置主键值
        collect.setUsername(id);
        //调用collectService实现修改Collect
        collectService.update(collect);
        return new Result(true,StatusCode.OK,"修改成功");
    }

    /***
     * 新增Collect数据
     * @param collect
     * @return
     */
    @PostMapping
    public Result add(@RequestBody   Collect collect){
        //调用CollectService实现添加Collect
        collectService.add(collect);
        return new Result(true,StatusCode.OK,"添加成功");
    }

    /***
     * 根据ID查询Collect数据
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result<Collect> findById(@PathVariable String id){
        //调用CollectService实现根据主键查询Collect
        Collect collect = collectService.findById(id);
        return new Result<Collect>(true,StatusCode.OK,"查询成功",collect);
    }

    /***
     * 查询Collect全部数据
     * @return
     */
    @GetMapping
    public Result<List<Collect>> findAll(){
        //调用CollectService实现查询所有Collect
        List<Collect> list = collectService.findAll();
        return new Result<List<Collect>>(true, StatusCode.OK,"查询成功",list) ;
    }



    /***
     * 根据username查询收藏中的商品数据
     * @param collect
     * @return
     */
    @GetMapping("/collection")
    public Result<List<Spu>> findByUsername(@RequestBody(required = false)  Collect collect){
        //调用CollectService实现根据条件查询Collect
        List<Spu> list = collectService.findByUsername(collect);
        return new Result<List<Spu>>(true,StatusCode.OK,"查询成功",list);
    }
}
